<?php
/**
 * QrCode.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-2 下午9:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

require_once(BASEPATH_LIBRARY . 'phpqrcode/phpqrcode.php');

class QrCode extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'QrCode',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'Type' => self::VAR_TYPE_INT,
            'TypeId' => self::VAR_TYPE_INT,
            'UserId' => self::VAR_TYPE_INT,
            'ShopId' => self::VAR_TYPE_INT,
            'QrCodeUrl' => self::VAR_TYPE_STRING,
            'Status' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'QrCode:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const QR_CODE_HOST = 'http://api.jinguonet.com/qrcode.php';
    // 1:活动 2：电子券 3：会员价 4：用户金果榜领奖
    const TYPE_ACT = 1;
    const TYPE_COUPON = 2;
    const TYPE_SHOP_CHAP = 3;
    const TYPE_REWARD_COIN = 4;

    const MC_USER_TYPE_LIST = 'Active_My_List:{ActiveId}:{UserId}';
    const UPDATE_DEL_CACHE_LIST = true; // 更新是否清除列表缓存
    // 列表缓存
    protected static $_cache_list_keys = [
        self::MC_USER_TYPE_LIST => [
            self::CACHE_LIST_TYPE => self::CACHE_LIST_TYPE_MC,
            self::CACHE_LIST_FIELDS => ['ActiveId', 'UserId'],
        ],
    ];

    /**
     * 获取我的二维码列表
     *
     * @param     $uid
     * @param int $type
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public static function getUserTypeCode($uid, $type = self::TYPE_SHOP_CHAP, $offset = 0, $limit = 20)
    {
        $where = [
            'UserId' => $uid,
            'Type' => $type
        ];
        $extra = [
            'type' => parent::MOD_TYPE_MEMCACHE,
            'mckey' => self::MC_USER_TYPE_LIST,
            'mtime' => 86400,
        ];
        $data = parent::getDataByFields($where, $extra);
        if (!empty($data['data'])) {
            $data['data'] = array_slice($data['data'], $offset, $limit);
        }

        return $data;
    }

    /**
     * 检测二维码是否存在
     *
     * @param $uid
     * @param $type
     * @param $type_id
     * @return array
     */
    public static function checkUserTypeCode($uid, $type, $type_id)
    {
        $where = [
            'UserId' => $uid,
            'Type' => $type,
            'TypeId' => $type_id,
        ];
        $info = self::getList($where, 0, 1);

        return isset($info['data'][0]) ? $info['data'][0] : [];
    }

    /**
     * 新增二维码
     *
     * @param     $type_id
     * @param     $user_id
     * @param int $type
     * @return bool|int|string
     */
    public static function addQrCode($type_id, $user_id, $type = self::TYPE_SHOP_CHAP)
    {
        $data = [
            'Type' => $type,
            'TypeId' => $type_id,
            'UserId' => $user_id,
            'Status' => 0,
            'CreateTime' => date('Y-m-d H:i:s'),
        ];

        $id = self::add($data);
        if (!empty($id)) {
            $qrcodeurl = self::getQrCodeUrl($id, $type_id, $user_id, $type);
            if (!empty($qrcodeurl)) {
                self::update($id, ['QrCodeUrl' => $qrcodeurl]);
            }
        }

        return self::add($data);
    }

    /**
     * 生成二维码
     *
     * @param $id
     * @param $type_id
     * @param $user_id
     * @param $type
     * @return mixed
     */
    public static function getQrCodeUrl($id, $type_id, $user_id, $type)
    {
        $query = [
            'Id' => $id,
            'TypeId' => $type_id,
            'UserId' => $user_id,
            'Type' => $type
        ];
        $url = self::QR_CODE_HOST . '?' . http_build_query($query);
        $path_info = getSavePath('qrcode', $id);
        //$rs = \qrcode::png($url, $path_info['path']);
        //$rs = \qrcode\QRcode::png($url, $path_info['path']);
        //var_dump($rs);
        return $path_info['Url'];
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}